package com.sky.aspect;

import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

@Slf4j
@Aspect
@Component
public class AutoFillAspect {
    //定义切入点
    @Pointcut(" execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointCut(){}

    /**
     * 前置通知，对公共字段进行数据填充
     */
    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinPoint){
        log.info("正在进行公共字段数据填充...");
        //获取当前拦截到的方法的数据库操作类型
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        AutoFill annotation = methodSignature.getMethod().getAnnotation(AutoFill.class);
        OperationType operationType = annotation.value();
        //获取当前拦截到的方法的参数--实体对象
        Object[] objects = joinPoint.getArgs();
        if(objects==null || objects.length==0){
            return;
        }
        Object object = objects[0];
        //准备赋值的数据
        LocalDateTime now = LocalDateTime.now();
        Long currentUser = BaseContext.getCurrentId();
        //根据不同的操作为对应属性进行赋值
        if (operationType==OperationType.INSERT){
            //为四个属性进行复制
            //获取方法
            try {
                Method setCreateTime = object.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setCreateUser=object.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER,Long.class);
                Method setUpdateTime=object.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME,LocalDateTime.class);
                Method setUpdateUser=object.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER,Long.class);
                //反射赋值
                setCreateTime.invoke(object,now);
                setUpdateTime.invoke(object,now);
                setCreateUser.invoke(object,currentUser);
                setUpdateUser.invoke(object,currentUser);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }else if(operationType==OperationType.UPDATE){
            //为两属性进行赋值
            try {
                Method setUpdateTime=object.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME,LocalDateTime.class);
                Method setUpdateUser=object.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER,Long.class);
                //反射赋值
                setUpdateTime.invoke(object,now);
                setUpdateUser.invoke(object,currentUser);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
